package com.qf.dao.impl;

import com.qf.dao.BookDao;
import com.qf.entity.Book;
import com.qf.utils.DruidUtil;
import com.qf.utils.Page;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/*
杨威
2021/9/13
21:28
*/
public class BookDaoImpl implements BookDao {
    QueryRunner queryRunner=new QueryRunner();
    @Override
    public int insert(Book book) {
        try {
            String sql="insert into book(btitle,bauthor,bpublicdate,bpublisher,bisbn,bprice) VALUES (?,?,?,?,?,?) ";
            Object[] params={book.getbTitle(),book.getbAuthor(),book.getbPublicDate(),book.getbPublisher(),book.getbIsbn(),book.getbPrice()};
            int i=queryRunner.update(DruidUtil.getConnection(),sql,params);
            return i;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"新增异常");
        }
    }

    @Override
    public int delete(int i) {
        try {
            String sql="delete from book where bid=?";
            int update = queryRunner.update(DruidUtil.getConnection(), sql, i);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"删除异常");
        }
    }

    @Override
    public int update(Book book) {
        try {
            String sql="update book set btitle=?,bauthor=?,bpublicdate=?,bpublisher=?,bisbn=?,bprice=? where bid=?";
            Object[] params={book.getbTitle(),book.getbAuthor(),book.getbPublicDate(),book.getbPublisher(),book.getbIsbn(),book.getbPrice(),book.getbId()};
            return queryRunner.update(DruidUtil.getConnection(), sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"修改异常");
        }
    }

    @Override
    public Book selectById(int i) {
        try {
            String sql="select * from book where bid=?";
            return queryRunner.query(DruidUtil.getConnection(),sql,new BeanHandler<>(Book.class),i);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询单个异常");
        }
    }

    @Override
    public List<Book> selectAll() {
        try {
            String sql="select * from book";
            return queryRunner.query(DruidUtil.getConnection(),sql,new BeanListHandler<>(Book.class));
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询所有书籍出现异常");
        }
    }

    @Override
    public Long selectCount() {
        try {
            String sql="select count(*) from book";
            return queryRunner.query(DruidUtil.getConnection(),sql,new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询数量异常");
        }
    }

    @Override
    public List<Book> selectAllOrderByDate() {
        try {
            String sql="select * from book order by bPublicDate";
            List<Book> query = queryRunner.query(DruidUtil.getConnection(), sql, new BeanListHandler<>(Book.class));
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询所有书籍根据日期排序出现异常！");
        }
    }

    @Override
    public List<Book> selectAllByPage(Page page) {
        try {
            String sql="select * from book limit ?,?";
            return queryRunner.query(DruidUtil.getConnection(),sql,new BeanListHandler<>(Book.class),page.getStartRow(),page.getPageSize()) ;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询书根据分页出现异常");
        }
    }
}
